///@file StressTensor.cpp
///@brief Implementation of the class representing a symmetric stress tensor
///@author Arnaud Duval
///@version 0.0
///@date 2010/02/25

#include "../include/StressTensor.h"
#include <cmath>


///@brief return Von Mises equivalent stress
double StressTensor::EquivVM()
{
	double trace = 0.;
	for(unsigned int i = 0 ; i < dim ; i++)
		trace += a[i][i+1];
	double res = 0.;
	for(unsigned int i = 0 ; i < dim ; i++)
	{
		for(unsigned int j = 0 ; j < i+1 ; j++)
		{
			if(i != j)
				res += 2.*pow(a[i][j+1],2.);
			else
				res += pow(a[i][j+1] - trace/((double)dim),2.);
		}
	}

	return sqrt(3./2. * res);
}

/*///@brief Cast operator to StressVector
StressTensor::operator StressVector()
{
	assert((dim == 2)||(dim == 3));
	StrainVector res;
	switch(dim)
	{
		case(3):
		{
			res = StrainVector(6u);
			res[1] = a[0][1];
			res[2] = a[1][2];
			res[3] = a[2][3];
			res[4] = 2.*a[1][1];
			res[5] = 2.*a[2][1];
			res[6] = 2.*a[2][2];
		}break;
		case(2):
		{
			res = StrainVector(3u);
			res[1] = a[0][1];
			res[2] = a[1][2];
			res[3] = 2.*a[1][1];
		}break;
	}
	return res;
}*/